16 #ifdef __MSP430_HAS_USCI_Bx__
21 void USCI_B_I2C_initMaster(uint16_t baseAddress, USCI_B_I2C_initMasterParam *
param)
23 uint16_t preScalarValue;
26 HWREG8(baseAddress + OFS_UCBxCTL1) = UCSWRST;
34 HWREG8(baseAddress + OFS_UCBxCTL0) = UCMST + UCMODE_3 + UCSYNC;
37 HWREG8(baseAddress + OFS_UCBxCTL1) = (
param->selectClockSource + UCSWRST );
45 preScalarValue = (
unsigned short)(
param->i2cClk /
param->dataRate);
46 HWREG16(baseAddress + OFS_UCBxBRW) = preScalarValue;
48 void USCI_B_I2C_initSlave (uint16_t baseAddress,
53 HWREG8(baseAddress + OFS_UCBxCTL1) |= UCSWRST;
56 HWREG8(baseAddress + OFS_UCBxCTL0) &= ~UCMST;
59 HWREG8(baseAddress + OFS_UCBxCTL0) = UCMODE_3 + UCSYNC;
62 HWREG16(baseAddress + OFS_UCBxI2COA) = slaveAddress;
65 void USCI_B_I2C_enable (uint16_t baseAddress)
68 HWREG8(baseAddress + OFS_UCBxCTL1) &= ~(UCSWRST);
71 void USCI_B_I2C_disable (uint16_t baseAddress)
74 HWREG8(baseAddress + OFS_UCBxCTL1) |= UCSWRST;
77 void USCI_B_I2C_setSlaveAddress (uint16_t baseAddress,
82 HWREG16(baseAddress + OFS_UCBxI2CSA) = (slaveAddress);
85 void USCI_B_I2C_setMode (uint16_t baseAddress,
89 HWREG8(baseAddress + OFS_UCBxCTL1) &= ~USCI_B_I2C_TRANSMIT_MODE;
90 HWREG8(baseAddress + OFS_UCBxCTL1) |= mode;
93 void USCI_B_I2C_slavePutData (uint16_t baseAddress,
101 uint8_t USCI_B_I2C_slaveGetData (uint16_t baseAddress)
104 return (
HWREG8(baseAddress + OFS_UCBxRXBUF));
107 uint8_t USCI_B_I2C_isBusBusy (uint16_t baseAddress)
110 return (
HWREG8(baseAddress + OFS_UCBxSTAT) & UCBBUSY);
113 uint8_t USCI_B_I2C_isBusy (uint16_t baseAddress)
116 if ((
HWREG8(baseAddress + OFS_UCBxIFG) & (UCTXIFG + UCRXIFG))){
117 return (USCI_B_I2C_BUS_BUSY);
119 return (USCI_B_I2C_BUS_NOT_BUSY);
123 uint8_t USCI_B_I2C_masterIsStopSent (uint16_t baseAddress)
126 return (
HWREG8(baseAddress + OFS_UCBxCTL1) & UCTXSTP);
129 uint8_t USCI_B_I2C_masterIsStartSent (uint16_t baseAddress)
132 return (
HWREG8(baseAddress + OFS_UCBxCTL1) & UCTXSTT);
135 void USCI_B_I2C_masterSendStart (uint16_t baseAddress)
137 HWREG8(baseAddress + OFS_UCBxCTL1) |= UCTXSTT;
140 void USCI_B_I2C_enableInterrupt (uint16_t baseAddress,
145 HWREG8(baseAddress + OFS_UCBxIE) |= mask;
148 void USCI_B_I2C_disableInterrupt (uint16_t baseAddress,
153 HWREG8(baseAddress + OFS_UCBxIE) &= ~(mask);
156 void USCI_B_I2C_clearInterrupt (uint16_t baseAddress,
161 HWREG8(baseAddress + OFS_UCBxIFG) &= ~(mask);
164 uint8_t USCI_B_I2C_getInterruptStatus (uint16_t baseAddress,
169 return (
HWREG8(baseAddress + OFS_UCBxIFG) & mask);
172 void USCI_B_I2C_masterSendSingleByte (uint16_t baseAddress,
177 uint8_t txieStatus =
HWREG8(baseAddress + OFS_UCBxIE) & UCTXIE;
180 HWREG8(baseAddress + OFS_UCBxIE) &= ~(UCTXIE);
183 HWREG8(baseAddress + OFS_UCBxCTL1) |= UCTR + UCTXSTT;
186 while (!(
HWREG8(baseAddress + OFS_UCBxIFG) & UCTXIFG)) ;
189 HWREG8(baseAddress + OFS_UCBxTXBUF) = txData;
192 while (!(
HWREG8(baseAddress + OFS_UCBxIFG) & UCTXIFG)) ;
195 HWREG8(baseAddress + OFS_UCBxCTL1) |= UCTXSTP;
198 HWREG8(baseAddress + OFS_UCBxIFG) &= ~(UCTXIFG);
201 HWREG8(baseAddress + OFS_UCBxIE) |= txieStatus;
204 bool USCI_B_I2C_masterSendSingleByteWithTimeout (uint16_t baseAddress,
210 uint32_t timeout2 = timeout;
213 uint8_t txieStatus =
HWREG8(baseAddress + OFS_UCBxIE) & UCTXIE;
216 HWREG8(baseAddress + OFS_UCBxIE) &= ~(UCTXIE);
219 HWREG8(baseAddress + OFS_UCBxCTL1) |= UCTR + UCTXSTT;
222 while ((!(
HWREG8(baseAddress + OFS_UCBxIFG) & UCTXIFG)) && --timeout) ;
230 HWREG8(baseAddress + OFS_UCBxTXBUF) = txData;
233 while ((!(
HWREG8(baseAddress + OFS_UCBxIFG) & UCTXIFG)) && --timeout2) ;
241 HWREG8(baseAddress + OFS_UCBxCTL1) |= UCTXSTP;
244 HWREG8(baseAddress + OFS_UCBxIFG) &= ~(UCTXIFG);
247 HWREG8(baseAddress + OFS_UCBxIE) |= txieStatus;
252 void USCI_B_I2C_masterSendMultiByteStart (uint16_t baseAddress,
257 uint8_t txieStatus =
HWREG8(baseAddress + OFS_UCBxIE) & UCTXIE;
260 HWREG8(baseAddress + OFS_UCBxIE) &= ~(UCTXIE);
263 HWREG8(baseAddress + OFS_UCBxCTL1) |= UCTR + UCTXSTT;
266 while (!(
HWREG8(baseAddress + OFS_UCBxIFG) & UCTXIFG)) ;
269 HWREG8(baseAddress + OFS_UCBxTXBUF) = txData;
272 HWREG8(baseAddress + OFS_UCBxIE) |= txieStatus;
275 bool USCI_B_I2C_masterSendMultiByteStartWithTimeout (uint16_t baseAddress,
281 uint8_t txieStatus =
HWREG8(baseAddress + OFS_UCBxIE) & UCTXIE;
284 HWREG8(baseAddress + OFS_UCBxIE) &= ~(UCTXIE);
287 HWREG8(baseAddress + OFS_UCBxCTL1) |= UCTR + UCTXSTT;
290 while ((!(
HWREG8(baseAddress + OFS_UCBxIFG) & UCTXIFG)) && --timeout) ;
298 HWREG8(baseAddress + OFS_UCBxTXBUF) = txData;
301 HWREG8(baseAddress + OFS_UCBxIE) |= txieStatus;
306 void USCI_B_I2C_masterSendMultiByteNext (uint16_t baseAddress,
311 if (!(
HWREG8(baseAddress + OFS_UCBxIE) & UCTXIE)){
313 while (!(
HWREG8(baseAddress + OFS_UCBxIFG) & UCTXIFG)) ;
317 HWREG8(baseAddress + OFS_UCBxTXBUF) = txData;
320 bool USCI_B_I2C_masterSendMultiByteNextWithTimeout (uint16_t baseAddress,
326 if (!(
HWREG8(baseAddress + OFS_UCBxIE) & UCTXIE)){
328 while ((!(
HWREG8(baseAddress + OFS_UCBxIFG) & UCTXIFG)) && --timeout);
337 HWREG8(baseAddress + OFS_UCBxTXBUF) = txData;
342 void USCI_B_I2C_masterSendMultiByteFinish (uint16_t baseAddress,
347 if (!(
HWREG8(baseAddress + OFS_UCBxIE) & UCTXIE)){
349 while (!(
HWREG8(baseAddress + OFS_UCBxIFG) & UCTXIFG)) ;
353 HWREG8(baseAddress + OFS_UCBxTXBUF) = txData;
356 while (!(
HWREG8(baseAddress + OFS_UCBxIFG) & UCTXIFG)) ;
359 HWREG8(baseAddress + OFS_UCBxCTL1) |= UCTXSTP;
362 bool USCI_B_I2C_masterSendMultiByteFinishWithTimeout (uint16_t baseAddress,
367 uint32_t timeout2 = timeout;
370 if (!(
HWREG8(baseAddress + OFS_UCBxIE) & UCTXIE)){
372 while ((!(
HWREG8(baseAddress + OFS_UCBxIFG) & UCTXIFG)) && --timeout) ;
381 HWREG8(baseAddress + OFS_UCBxTXBUF) = txData;
384 while ((!(
HWREG8(baseAddress + OFS_UCBxIFG) & UCTXIFG)) && --timeout2) ;
392 HWREG8(baseAddress + OFS_UCBxCTL1) |= UCTXSTP;
397 void USCI_B_I2C_masterSendMultiByteStop (uint16_t baseAddress)
400 if (!(
HWREG8(baseAddress + OFS_UCBxIE) & UCTXIE)){
402 while (!(
HWREG8(baseAddress + OFS_UCBxIFG) & UCTXIFG)) ;
406 HWREG8(baseAddress + OFS_UCBxCTL1) |= UCTXSTP;
409 bool USCI_B_I2C_masterSendMultiByteStopWithTimeout (uint16_t baseAddress,
413 if (!(
HWREG8(baseAddress + OFS_UCBxIE) & UCTXIE)){
415 while ((!(
HWREG8(baseAddress + OFS_UCBxIFG) & UCTXIFG)) && --timeout) ;
424 HWREG8(baseAddress + OFS_UCBxCTL1) |= UCTXSTP;
429 void USCI_B_I2C_masterReceiveMultiByteStart (uint16_t baseAddress)
432 HWREG8(baseAddress + OFS_UCBxCTL1) &= ~UCTR;
434 HWREG8(baseAddress + OFS_UCBxCTL1) |= UCTXSTT;
437 uint8_t USCI_B_I2C_masterReceiveMultiByteNext (uint16_t baseAddress)
439 return (
HWREG8(baseAddress + OFS_UCBxRXBUF));
442 uint8_t USCI_B_I2C_masterReceiveMultiByteFinish (uint16_t baseAddress)
447 HWREG8(baseAddress + OFS_UCBxCTL1) |= UCTXSTP;
454 while (
HWREG8(baseAddress + OFS_UCBxCTL1) & UCTXSTP);
457 while (!(
HWREG8(baseAddress + OFS_UCBxIFG) & UCRXIFG));
462 bool USCI_B_I2C_masterReceiveMultiByteFinishWithTimeout (uint16_t baseAddress,
467 uint32_t timeout2 = timeout;
470 HWREG8(baseAddress + OFS_UCBxCTL1) |= UCTXSTP;
474 *rxData = (
HWREG8(baseAddress + OFS_UCBxRXBUF));
477 while ((
HWREG8(baseAddress + OFS_UCBxCTL1) & UCTXSTP) && --timeout);
485 while ((!(
HWREG8(baseAddress + OFS_UCBxIFG) & UCRXIFG)) && --timeout2) ;
495 void USCI_B_I2C_masterReceiveMultiByteStop (uint16_t baseAddress)
498 HWREG8(baseAddress + OFS_UCBxCTL1) |= UCTXSTP;
501 void USCI_B_I2C_masterReceiveSingleStart (uint16_t baseAddress)
507 gieStatus = __get_SR_register() & GIE;
510 __disable_interrupt();
513 HWREG8(baseAddress + OFS_UCBxCTL1) &= ~UCTR;
516 HWREG8(baseAddress + OFS_UCBxCTL1) |= UCTXSTT;
519 while (
HWREG8(baseAddress + OFS_UCBxCTL1) & UCTXSTT) ;
522 HWREG8(baseAddress + OFS_UCBxCTL1) |= UCTXSTP;
525 __bis_SR_register(gieStatus);
528 bool USCI_B_I2C_masterReceiveSingleStartWithTimeout (uint16_t baseAddress,
536 gieStatus = __get_SR_register() & GIE;
539 __disable_interrupt();
542 HWREG8(baseAddress + OFS_UCBxCTL1) &= ~UCTR;
545 HWREG8(baseAddress + OFS_UCBxCTL1) |= UCTXSTT;
548 while (((
HWREG8(baseAddress + OFS_UCBxCTL1) & UCTXSTT)) && --timeout);
556 HWREG8(baseAddress + OFS_UCBxCTL1) |= UCTXSTP;
559 __bis_SR_register(gieStatus);
564 uint8_t USCI_B_I2C_masterReceiveSingle (uint16_t baseAddress)
567 if(!(
HWREG8(baseAddress + OFS_UCBxIE) & UCRXIE)) {
568 while(!(
HWREG8(baseAddress + OFS_UCBxIFG) & UCRXIFG));
572 return (
HWREG8(baseAddress + OFS_UCBxRXBUF));
575 uint32_t USCI_B_I2C_getReceiveBufferAddressForDMA (uint16_t baseAddress)
577 return ( baseAddress + OFS_UCBxRXBUF );
580 uint32_t USCI_B_I2C_getTransmitBufferAddressForDMA (uint16_t baseAddress)
582 return ( baseAddress + OFS_UCBxTXBUF );
MPU_initThreeSegmentsParam param